package com.xunce.xcworkservice.response;

import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

/**
 * @author yanlong.guo
 * 统一结果集处理器
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
@Tag(name = "R", description = "统一结果集处理器")
public class R<T> implements Serializable {

    /**
     * 状态码
     */
    @Schema(name = "code", description = "错误码 0 200 都为正常状态 其他异常")
    private Integer code;

    /**
     * 信息
     */
    @Schema(name = "message", description = "返回信息")
    private String message;

    /**
     * 数据
     */
    @Schema(name = "data", description = "返回数据")
    private T data;

    /**
     * 全参数方法
     *
     * @param code    状态码
     * @param message 返回信息
     * @param data    返回数据
     * @param <T>     泛型
     * @return {@link R<T>}
     */
    private static <T> R<T> response(Integer code, String message, T data) {
        R<T> responseResult = new R<>();
        responseResult.setCode(code);
        responseResult.setMessage(message);
        responseResult.setData(data);
        return responseResult;
    }
    /**
     * 全参数方法
     *
     * @param code    状态码
     * @param message 返回信息
     * @return {@link R<T>}
     */
    private static <T> R<T> response(Integer code, String message) {
        R<T> responseResult = new R<>();
        responseResult.setCode(code);
        responseResult.setMessage(message);
        return responseResult;
    }

    /**
     * 成功返回（无参）
     *
     * @param <T> 泛型
     * @return {@link R<T>}
     */
    public static <T> R<T> success() {
        return response(ResultCode.OK.getCode(), ResultCode.OK.getMsg());
    }

    public static <T> R<T> success(ResultCode resultCode) {
        return response(resultCode.getCode(), resultCode.getMsg());
    }

    public static <T> R<T> success(T data) {
        return response(ResultCode.OK.getCode(), ResultCode.OK.getMsg(), data);
    }

    public static <T> R<T> error() {
        return response(ResultCode.FAIL.getCode(), ResultCode.FAIL.getMsg());
    }

    public static <T> R<T> error(String msg) {
        return response(ResultCode.FAIL.getCode(), msg);
    }

    public static <T> R<T> error(Integer code, String msg) {
        return response(code, msg);
    }

    public static <T> R<T> error(Integer code, String msg, T data) {
        return response(code, msg, data);
    }
    public static <T> R<T> error(ResultCode resultCode) {
        return response(resultCode.getCode(), resultCode.getMsg());
    }

    public static <T> R<T> error(T data) {
        return response(ResultCode.FAIL.getCode(), ResultCode.FAIL.getMsg(), data);
    }
}
